package com.yanqu.road.server.manger.cricket.skillhandler.startround.handler;

import com.yanqu.road.entity.cricket.helper.AddHpResult;
import com.yanqu.road.entity.enums.eSkillType;
import com.yanqu.road.pb.cricket.CricketProto;
import com.yanqu.road.server.gameplayer.module.cricket.battle.CricketGame;
import com.yanqu.road.server.gameplayer.module.cricket.battle.eCricketBattleRoundStep;
import com.yanqu.road.server.gameplayer.module.cricket.model.BattleData;
import com.yanqu.road.server.gameplayer.module.cricket.model.SkillData;
import com.yanqu.road.server.manger.cricket.skillhandler.startround.CricketSkillStartRoundStrategy;
import com.yanqu.road.utils.RandomHelper;

import java.math.BigDecimal;
import java.math.RoundingMode;

public class CricketSkill260Strategy implements CricketSkillStartRoundStrategy {

    /**
     * 技能处理-九色鹿恶有恶报
     * @param cricketGame 本局对局对象
     * @param skillData 技能数据
     * @param action 我方
     * @param target 对方
     * @param roundId 第几回合
     * @param attackRoundBuilder 返回PB结构体
     * @return
     */
    @Override
    public boolean skillHandle(CricketGame cricketGame, SkillData skillData, BattleData action, BattleData target, int roundId, CricketProto.CricketAttackRound.Builder attackRoundBuilder) {
        //判断敌方是否是九色鹿
        boolean jslFlag = false;
        for (SkillData actionSkillData : action.getSkillDataList()) {
            if (actionSkillData.getSkillEffect().getSkillType() == eSkillType.CricketBattleSkill260.getValue()) {
                jslFlag = true;
                break;
            }
        }
        //判断回合数是否到达，且敌方不是九色鹿
        int targetRound = skillData.getSkillEffect().getP4();
        if (roundId >= targetRound && !jslFlag) {
            //混火烧身.敌人会受到自身生命上限*10的伤害。如果敌人也是九色鹿，不会受到该伤害
            skillData.effectAmountInc();
            //攻击方减血
            long reHp = action.getMaxHp() * 10;
            AddHpResult addHpResult = action.addHp(-reHp);
            //封装返回数据
            CricketProto.CricketBattleFrame.Builder newBattleFrameBuilder = cricketGame.newBattleFrameBuilder();
            newBattleFrameBuilder.setActionUserId(target.getUserId());
            newBattleFrameBuilder.setTargetUserId(action.getUserId());
            newBattleFrameBuilder.setSkillId(skillData.getSkillId());
            newBattleFrameBuilder.setAttack(addHpResult.getValue());
            newBattleFrameBuilder.setIsKill(addHpResult.isKill());
            newBattleFrameBuilder.setHarmEffectSkillId(addHpResult.getReduceHpEffectSkillId());
            newBattleFrameBuilder.setIsMiss(addHpResult.isMissFlag());
            cricketGame.fillRemainHp(newBattleFrameBuilder);
            attackRoundBuilder.addStartRound(newBattleFrameBuilder);
            //是否结束
            if (cricketGame.isOver(attackRoundBuilder, eCricketBattleRoundStep.StartRound.getValue(), newBattleFrameBuilder.getBattleFrameId())) {
                return true;
            }
        } else {
            //会受到良心的谴责
            //判断是否触发
            int useRate = skillData.getSkillEffect().getP3();
            int realRate = RandomHelper.rand1000();
            if (realRate < useRate) {
                skillData.effectAmountInc();
                //攻击方减血
                long reHp = BigDecimal.valueOf(target.getAttack(action)).multiply(BigDecimal.valueOf(skillData.getSkillEffect().getP1())).divide(BigDecimal.valueOf(1000), 8, RoundingMode.DOWN).longValue();
                AddHpResult addHpResult = action.addHp(-reHp);
                //攻击方减气势
                long rePower = BigDecimal.valueOf(action.getPower()).multiply(BigDecimal.valueOf(skillData.getSkillEffect().getP2())).divide(BigDecimal.valueOf(1000), 8, RoundingMode.DOWN).longValue();
                action.addPower(-rePower);
                //封装返回数据
                CricketProto.CricketBattleFrame.Builder newBattleFrameBuilder = cricketGame.newBattleFrameBuilder();
                newBattleFrameBuilder.setActionUserId(target.getUserId());
                newBattleFrameBuilder.setTargetUserId(action.getUserId());
                newBattleFrameBuilder.setSkillId(skillData.getSkillId());
                newBattleFrameBuilder.setAttack(addHpResult.getValue());
                newBattleFrameBuilder.setIsKill(addHpResult.isKill());
                newBattleFrameBuilder.setTargetChangePower(-rePower);
                newBattleFrameBuilder.setHarmEffectSkillId(addHpResult.getReduceHpEffectSkillId());
                newBattleFrameBuilder.setIsMiss(addHpResult.isMissFlag());
                cricketGame.fillRemainHp(newBattleFrameBuilder);
                attackRoundBuilder.addStartRound(newBattleFrameBuilder);
                //是否结束
                if (cricketGame.isOver(attackRoundBuilder, eCricketBattleRoundStep.StartRound.getValue(), newBattleFrameBuilder.getBattleFrameId())) {
                    return true;
                }
            }
        }
        return false;
    }
}
